package com.pet.utils;

import java.nio.ByteBuffer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by candy.zhao on 2016-11-8.
 */
public class EmojiFilter {

    /**
     * 检测是否有emoji字符
     * zg
     *
     * @param source
     * @return 一旦含有就抛出
     */
    public static boolean containsEmoji(String source) {
        if (source == null || "".equals(source)) {
            return false;
        }
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);

            if (isEmojiCharacter(codePoint)) {
                //do nothing，判断到了这里表明，确认有表情字符
                return true;
            }
        }

        return false;
    }

    /**
     * 将emoji表情替换成*
     * @param source
     * @return 过滤后的字符串
     */
    public static String filterEmoji2(String source) {
        if(source == null || "".equals(source)) return source;
        source = filterEmoji(source);
        Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE ) ;
        Matcher emojiMatcher = emoji.matcher(source);
        if (emojiMatcher.find()){
            source = emojiMatcher.replaceAll("*");
        }
//        return filterOffUtf8Mb4(source);
        return source;
    }

    static public String filterOffUtf8Mb4(String text) {
        try {
            byte[] bytes = text.getBytes("utf-8");
            ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
            int i = 0;
            while (i < bytes.length) {
                short b = bytes[i];
                if (b > 0) {
                    buffer.put(bytes[i++]);
                    continue;
                }
                b += 256;
                if ((b ^ 0xC0) >> 4 == 0) {
                    buffer.put(bytes, i, 2);
                    i += 2;
                } else if ((b ^ 0xE0) >> 4 == 0) {
                    buffer.put(bytes, i, 3);
                    i += 3;
                } else if ((b ^ 0xF0) >> 4 == 0) {
                    i += 4;
                }
            }
            buffer.flip();
            return new String(buffer.array(), "utf-8");
        }catch (Exception e){
            return text;
        }
    }

    /**
     * 根据Unicode 6字符标准来判断一个字符是否为emoji字符
     * zg
     *
     * @param codePoint
     * @return 一旦含有就抛出
     */
    private static boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
    }

    /**
     * 过滤emoji 或者 其他非文字类型的字符
     *
     * @param source
     * @return
     */
    public static String filterEmoji(String source) {
        if (source == null || "".equals(source)) {
            return source;
        }
        //到这里铁定包含
        StringBuilder buf = null;
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (isEmojiCharacter(codePoint)) {
                if (buf == null) {
                    buf = new StringBuilder(source.length());
                }
                buf.append(codePoint);
            }
        }
        if (buf == null) {
            return source;//如果没有找到 emoji表情，则返回源字符串
        } else {
            if (buf.length() == len) {//这里的意义在于尽可能少的toString，因为会重新生成字符串
                buf = null;
                return source;
            } else {
                return buf.toString();
            }
        }
    }

    public static void main(String[] args){
        String title =
                "ff的范德萨分😁😁😁😁Llfldakf;dsk。f😁😁😁😁😁😁daslfjdsa;lfkjdsd'j'l'f'k'd'j'sa'l'k";
        System.out.println(filterEmoji2(title));
        System.out.println(filterOffUtf8Mb4(title));

        byte[] bytes = new byte[]{0x30, Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MIN_VALUE};

        System.out.println(new String(bytes));
        System.out.println(filterEmoji2(new String(bytes)));
    }
}
